import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';


const modules = import.meta.glob('../pages/admin/**/*.page.vue', {import: 'default'});
const routes: Array<RouteRecordRaw> = [];
const cacheRouterList: string[] = [];
const pageNames: AnyObject = {};
export const pscModule: RouteRecordRaw = {
    path: '/admin',
    name: 'admin',
    component: () => import('@/pages/admin/index.vue'),
    redirect: '/admin/home',
    children: [],
};


/**    */
Object.keys(modules).forEach((item) => {
    const reg = /[A-Za-z._-]+.page.vue/ig;
    const pageName = reg.exec(item) as RegExpExecArray;
    const text = pageName[0].replace('.page.vue', '');
    if (typeof text === 'string' && pageNames[text]) {
        console.error(`存在相同的路由中径，请重命名${text}`);
        return;
    }
    pscModule.children.push({
        path: text,
        name: text,
        component: modules[item],
    });
    cacheRouterList.push(text);
});





routes.push(pscModule);

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});


export const cachesRouter = cacheRouterList;
export default router;
